<script setup>
import { Edit, Delete } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { ref } from "vue";

//文章分类数据模型
const warehouse = ref([
  {
    warehouseID: 3,
    name: "仓库1",
    address: "地址",
    contact: "联系人",
    contactNumber: 18810857781,
  },
]);

//用户搜索时选中的分类id
const productid = ref("");

//用户搜索时选中的发布状态
const state = ref("");

//文章列表数据模型
const products = ref([
  {
    productID: 1,
    name: "螺丝",
    WarehouseBelong: "1",
    category: "杂项",
    stockQuantity: 2,
    source: "北交大",
    state: 1,
  },
]);

//分页条数据模型
const pageNum = ref(1); //当前页
const total = ref(20); //总条数
const pageSize = ref(3); //每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size;
};
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num;
};
// 从api中导入
import {
  goodsListService,
  goodsAddService,
  goodsUpdateService,
  goodsDeleteService,
  goodsInService,
  goodsOutService,
  goodsSearchService,
} from "@/api/goods.js";

const goodsList = async () => {
  let result = await goodsListService();
  // if(result.code===1){
  // warehouse.value = result.data;
  // }else{
  // ElMessage.error('获取数据失败')
  // }
  products.value = result.data;
};
goodsList();

//添加仓库的内容
//控制添加分类弹窗
const dialogVisible = ref(false);
const inoutVisible = ref(false);

//添加货物数据模型
const productModel = ref({
  name: "",
  WarehouseBelong: 1,
  category: "",
  stockQuantity: 1,
  source: "",
  inboundNumber: 1,
  outboundNumber: 1,
  productID: 1,
});
//添加分类表单校验
const rules = {
  name: [{ required: true, message: "请输入货物名称", trigger: "blur" }],
  WarehouseBelong: [
    { required: true, message: "请输入仓库id", trigger: "blur" },
  ],
  category: [{ required: true, message: "请输入类型", trigger: "blur" }],
  stockQuantity: [
    { required: true, message: "请输入库存数量", trigger: "blur" },
  ],
  source: [{ required: true, message: "请输入来源", trigger: "blur" }],
};

//调用接口，添加仓库
const addGoods = async () => {
  //调用接口
  let result = await goodsAddService(productModel.value);
  ElMessage.success(result.msg ? result.msg : "添加成功");

  //调用获取所有商品的接口
  goodsList();
  dialogVisible.value = false;
};

//定义变量，控制标题的展示
const title = ref("");
// 展示编辑弹窗
const showDialog = (row) => {
  dialogVisible.value = true;
  title.value = "编辑货物";
  //将当前行的数据赋值给warehouseModel
  productModel.value.name = row.name;
  productModel.value.warehouseBelong = row.warehouseBelong;
  productModel.value.category = row.category;
  productModel.value.stockQuantity = row.stockQuantity;
  productModel.value.source = row.source;
  // 扩展id属性，将来需要传递给后台
  productModel.value.productID = row.productID;
};

//编辑仓库
const updateGood = async () => {
  // 调用接口
  let result = await goodsUpdateService(productModel.value);

  ElMessage.success(result.msg ? result.msg : "编辑成功");

  //调用获取所有仓库的接口
  goodsList();
  dialogVisible.value = false;
};
//清空模型的数据
const clearData = () => {
  productModel.value = {
    name: "",
    WarehouseBelong: 1,
    category: "",
    stockQuantity: 1,
    source: "",
  };
};

// 入库
const inoutDialog = (row) => {
  inoutVisible.value = true;
  title.value = "入库";
  productModel.value.stockQuantity = row.stockQuantity;
  productModel.value.productID = row.productID;
};
//编辑仓库
const inGood = async () => {
  // 调用接口
  let result = await goodsInService(productModel.value);

  ElMessage.success(result.msg ? result.msg : "编辑成功");

  //调用获取所有仓库的接口
  goodsList();
  inoutVisible.value = false;
};

// 出库
const outGoodDialog = (row) => {
  inoutVisible.value = true;
  title.value = "出库";
  productModel.value.stockQuantity = row.stockQuantity;
  productModel.value.productID = row.productID;
};
//编辑仓库
const outGood = async () => {
  // 调用接口
  let result = await goodsOutService(productModel.value);

  ElMessage.success(result.msg ? result.msg : "编辑成功");

  //调用获取所有仓库的接口
  goodsList();
  inoutVisible.value = false;
};

// 删除货物
const deleteGood = (row) => {
  // 提示用户 确认删除吗？ 确认框
  ElMessageBox.confirm("你确认要删除该仓库吗？", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      //调用接口
      let result = await goodsDeleteService(row.productID);
      ElMessage({
        type: "success",
        message: "完成删除",
      });
      // 刷新列表
      goodsList();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

// 根据ID获取单个货物
const searchGoods = async () => {
  let result = await goodsSearchService(productModel.value);
  ElMessage.success(result.msg ? result.msg : "查询成功");

  // warehouse.value[0] = result.data;
  // 清空 warehouse.value
  products.value.splice(0);

  // 将搜索到的数据添加到列表中
  products.value.push(result.data);
};
</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>货物管理</span>
        <div class="extra">
          <el-button
            type="primary"
            @click="
              dialogVisible = true;
              title = '添加货物';
              clearData();
            "
            >添加货物</el-button
          >
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="货物id：">
        <el-select placeholder="请选择" v-model="productModel.productID">
          <el-option
            v-for="c in products"
            :key="c.id"
            :label="c.productID"
            :value="c.productID"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchGoods">搜索</el-button>
        <el-button @click="goodsList">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="products" style="width: 100%">
      <el-table-column
        label="货物的id"
        width="400"
        prop="productID"
      ></el-table-column>
      <el-table-column label="货物名" prop="name"></el-table-column>
      <el-table-column label="所属仓库名" prop="warehouseBelong">
      </el-table-column>
      <el-table-column label="类型" prop="category"></el-table-column>
      <el-table-column label="余量" prop="stockQuantity"></el-table-column>
      <el-table-column label="来源" prop="source"></el-table-column>
      <el-table-column label="状态" prop="state">
        <template v-slot="{ row }">
          <!-- <span v-if="row.state === 1">未入库</span> -->
          <el-text v-if="row.state === 1" class="mx-1" type="danger">未入库</el-text>
          <!-- <span v-if="row.state === 2">入库中</span> -->
          <el-text v-if="row.state === 2" class="mx-1" type="warning">入库中</el-text>
          <!-- <span v-if="row.state === 3">已入库</span> -->
          <el-text v-if="row.state === 3" class="mx-1" type="primary">已入库</el-text>
        </template>
      </el-table-column>

      <el-table-column label="出入库">
        <template #default="{ row }">
          <el-button type="primary" @click="inoutDialog(row)">入库</el-button>
          <el-button type="warning" @click="outGoodDialog(row)">出库</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="showDialog(row)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="deleteGood(row)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form
        :model="productModel"
        :rules="rules"
        label-width="100px"
        style="padding-right: 30px"
      >
        <el-form-item label="货物名称" prop="name">
          <el-input
            v-model="productModel.name"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属仓库id" prop="warehouseBelong">
          <el-input
            v-model="productModel.warehouseBelong"
            minlength="1"
            maxlength="15"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="category">
          <el-input
            v-model="productModel.category"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="余量" prop="stockQuantity">
          <el-input
            v-model="productModel.stockQuantity"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input
            v-model="productModel.source"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="title == '添加货物' ? addGoods() : updateGood()"
          >
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 出库入库弹窗 -->
    <el-dialog v-model="inoutVisible" :title="title" width="30%">
      <el-form
        :model="productModel"
        :rules="rules"
        label-width="100px"
        style="padding-right: 30px"
      >
        <el-form-item label="余量" prop="stockQuantity">
          <el-text
            type="info"
            v-model="productModel.stockQuantity"
            minlength="1"
            maxlength="10"
            >{{ productModel.stockQuantity }}</el-text
          >
        </el-form-item>
        <el-form-item label="入库" v-if="title === '入库'">
          <el-input
            v-model="productModel.inboundNumber"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="出库" v-if="title === '出库'">
          <el-input
            v-model="productModel.outboundNumber"
            minlength="1"
            maxlength="10"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button
            type="primary"
            @click="title == '入库' ? inGood() : outGood()"
          >
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 分页条 -->
    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 15]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .el-select {
    width: 150px;
  }
}
</style>